# app/api/trading/views.py
from flask_restx import Namespace, Resource, reqparse
from .modles import create_order, query_personal_trading_history, cancel_order

trading_ns = Namespace('trading_ns', description='用于对需要操控账户的交易的接口')

parser = reqparse.RequestParser()
parser.add_argument('gate_key', required=True, type=str, help='Gate.io API Key')
parser.add_argument('gate_secret', required=True, type=str, help='Gate.io API Secret')
parser.add_argument('settle', required=True, type=str, help='结算货币')
parser.add_argument('contract', required=True, type=str, help='合约名')
parser.add_argument('size', required=True, type=int, help='合约大小')
parser.add_argument('price', required=True, type=float, help='价格')
parser.add_argument('text', required=True, type=str, help='自定义订单标识')
parser.add_argument('tif', type=str, help='立即成交或取消', default='gtc')
parser.add_argument('close', type=bool, help='执行平仓操作', default=False)
parser.add_argument('auto_size', type=str, help='双仓模式下的自动平仓侧', choices=['close_long', 'close_short', ''])
parser.add_argument('reduce_only', type=bool, help='是否仅限平仓操作', default=False)

@trading_ns.route('/create_order')
class CreateOrderResource(Resource):
    @trading_ns.expect(parser)
    def post(self):
        """
        对应账号合约的下单操作
        """
        args = parser.parse_args()
        response = create_order(
            gate_key=args['gate_key'],
            gate_secret=args['gate_secret'],
            settle=args['settle'],
            contract=args['contract'],
            size=args['size'],
            price=args['price'],
            tif=args['tif'],
            close=args['close'],
            text=args['text'],
            auto_size=args['auto_size'],
            reduce_only=args['reduce_only']
        )
        return response, response['code']



# 查询个人交易历史的请求参数解析器
history_parser = reqparse.RequestParser()
history_parser.add_argument('gate_key', required=True, help='Gate.io API key')
history_parser.add_argument('gate_secret', required=True, help='Gate.io API secret')
history_parser.add_argument('settle', required=True, help='结算货币')
history_parser.add_argument('contract', help='合约标识')
history_parser.add_argument('order', type=int, help='订单ID')
history_parser.add_argument('limit', type=int, default=100, help='最大记录数')
history_parser.add_argument('offset', type=int, default=0, help='列表偏移量')
history_parser.add_argument('last_id', help='上一次查询的最后一条记录ID')


@trading_ns.route('/personal_trading_history')
class PersonalTradingHistory(Resource):
    @trading_ns.expect(history_parser)
    def post(self):
        """
        查询个人交易历史
        """
        args = history_parser.parse_args()
        response = query_personal_trading_history(
            gate_key=args['gate_key'], gate_secret=args['gate_secret'], settle=args['settle'],
            contract=args.get('contract'), order=args.get('order'), limit=args['limit'], offset=args['offset'],
            last_id=args.get('last_id')
        )
        return response, response.get("code", 200)


# 查询个人交易历史的请求参数解析器
cancel_order_parser = reqparse.RequestParser()
cancel_order_parser.add_argument('gate_key', required=True, help='Gate.io API Key')
cancel_order_parser.add_argument('gate_secret', required=True, help='Gate.io API Secret')
cancel_order_parser.add_argument('settle', required=True, help='结算货币')


@trading_ns.route('/cancel_order/<string:order_id>')
class CancelOrderResource(Resource):
    @trading_ns.expect(cancel_order_parser)
    def delete(self, order_id):
        """
        取消指定的订单
        """
        args = cancel_order_parser.parse_args()
        response = cancel_order(args['gate_key'], args['gate_secret'], args['settle'], order_id)
        return response, response['code']
